package com.lyon.custom.api.model;

import cn.hutool.http.HttpStatus;
import lombok.Data;

/**
 * @author LeeYan9
 * @since 2021-11-29
 */
@Data
public class CommonResult<T> {

    public static final String SUCCESS_MSG = "操作成功";

    private int code;

    private String message;

    private T data;

    private CommonResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> CommonResult<T> error(int code, String message, T data) {
        if (!checkError(code)) {
            return new CommonResult(HttpStatus.HTTP_INTERNAL_ERROR, "内部状态码错误", data);
        }
        return new CommonResult<>(code, message, data);
    }

    public static <T> CommonResult<T> success() {
        return success(null);
    }

    public static <T> CommonResult<T> success(T data) {
        return success(SUCCESS_MSG, data);
    }

    public static <T> CommonResult<T> success(String message, T data) {
        return new CommonResult<>(HttpStatus.HTTP_OK, message, data);
    }

    private static boolean checkError(int error) {
        return error != HttpStatus.HTTP_OK;
    }
}
